﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BackUpAnyWhere
{
    public partial class PopUpForm : Form
    {
        private int X = 0;
        private int Y = 0;
        private int NumOfRaws = 0;
        private static int CancelTimerCount = 10;
        public static int currentRunningCase = 0;
        private int[] FileCounter = new int[7];
        public List<string> FileToDeleteList = new List<string>();

        public PopUpForm(int Stat, int[] FileCount, List<string> DeleteList)
        {
            for (int index = 0; index < 7; index++)
                FileCounter[index] = FileCount[index];

            FileToDeleteList.AddRange(DeleteList);

            currentRunningCase = Stat;
            InitializeComponent();

            if (currentRunningCase == (int)BackUpAnyWhere.Program.UseCase.UC3)
                this.ClientSize = new System.Drawing.Size(292, 68);
        }

        private void PopUpForm_OnMouseLeave(object sender, System.EventArgs e)
        {
            DisplayTimer.Start();
        }

        private void PopUpForm_OnMouseEnter(object sender, System.EventArgs e)
        {
            DisplayTimer.Stop();
        }

        private void PopUpMessageLabel_OnMouseEnter(object sender, System.EventArgs e)
        {
            DisplayTimer.Stop();
        }

        private void InformationLabel_OnMouseEnter(object sender, System.EventArgs e)
        {
            DisplayTimer.Stop();
        }

        private void InformationLabel_OnMouseClick(object sender, System.EventArgs e) 
        {
            DisplayTimer.Stop();
            BackUpAnyWhere.FileHandle.DeleteFileList(FileToDeleteList);
            DisplayTimer.Start();
            InformationLabel.ForeColor = Color.MediumBlue;
            InformationLabel.Text = "\nFiles ware deleted correctly as requested.";
        }

        private void LoadingTimer_Tick(object sender, EventArgs e)
        {
            int i = this.Location.Y;
            if (i > Y - this.Height)
                this.Location = new Point(X - this.Width, i - 8);
            else
            {
                LoadingTimer.Stop();
                LoadingTimer.Enabled = false;

                DisplayTimer.Start();
                DisplayTimer.Enabled = true;
            }
        }

        private void UnloadTimer_Tick(object sender, EventArgs e)
        {
            DisplayTimer.Stop();
            DisplayTimer.Enabled = false;

            int i = this.Location.Y;
            if (i < Y)
                this.Location = new Point(X - this.Width, i + 8);
            else
            {
                UnloadTimer.Stop();
                UnloadTimer.Enabled = false;
                currentRunningCase = (int)BackUpAnyWhere.Program.UseCase.UC3_1;
                this.Close();
            }
        }

        private void DisplayTimer_Tick(object sender, EventArgs e)
        {
            UnloadTimer.Start();
            UnloadTimer.Enabled = true;
        }

        private void PopUpForm_Load(object sender, EventArgs e)
        {
            if (currentRunningCase != (int)BackUpAnyWhere.Program.UseCase.UC3)
            {
                string UserMessage = "";

                X = Screen.GetWorkingArea(this).Width;
                Y = Screen.GetWorkingArea(this).Height;
                this.Location = new Point(X - this.Width, Y + this.Height);
                this.Text = "BackUp AnyWhere©";

                if (currentRunningCase == (int)BackUpAnyWhere.Program.UseCase.UC1 || 
                    currentRunningCase == (int)BackUpAnyWhere.Program.UseCase.UC1_1)
                        UserMessage = "BackUpAnyWhere action was 'BackUp'\n";

                else if (currentRunningCase == (int)BackUpAnyWhere.Program.UseCase.UC2 ||
                         currentRunningCase == (int)BackUpAnyWhere.Program.UseCase.UC3_2)
                        UserMessage = "BackUpAnyWhere action was 'Synchronize'\n";

                else if (currentRunningCase == (int)BackUpAnyWhere.Program.UseCase.UC3_1)
                        UserMessage = "BackUpAnyWhere action was 'Automated Sync'\n";

                UserMessage = UserMessage + "Found " + FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileExist] + " files on flash drive\n\n";
                if (FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileSync] > 0)
                {
                    NumOfRaws++;
                    UserMessage = UserMessage + FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileSync] + " files of " + FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileExist] + " were sync\n";
                }
                if (FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileCopy] > 0)
                {
                    NumOfRaws++;
                    UserMessage = UserMessage + FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileCopy] + " files of " + FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileExist] + " were copied\n";
                }
                if (FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileNotCopeid] > 0)
                {
                    NumOfRaws++;
                    UserMessage = UserMessage + FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileNotCopeid] + " files of " + FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileExist] + " haven't change\n";
                }
                if (FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileCouldNotCopeid] > 0)
                {
                    NumOfRaws++;
                    GoodIconBox.Visible = false;
                    InformationMarkBox.Visible = true;
                    UserMessage = UserMessage + FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileCouldNotCopeid] + " files of " + FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileExist] + " couldn't be copied - Destination file is in use\n";
                }

                this.Height = this.Height + NumOfRaws * 12;
                UserMessage = UserMessage + "\nThank you for using BackUp AnyWhere©\n";

                if (FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileToDelete] != 0)
                {
                    this.Height = this.Height + 35;
                    InformationLabel.Location = new Point(InformationLabel.Location.X, PopUpMessageLabel.Location.Y + (NumOfRaws + 8) * 10);

                    DisplayTimer.Interval = 10000;
                    InformationMarkBox.Visible = true;
                    InformationLabel.ForeColor = Color.BlueViolet;
                    InformationLabel.Visible = true;
                    if (FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileToDelete] == 1)
                        InformationLabel.Text = "There is " + FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileToDelete] + " old file at the destination.\n" +
                                                "In order to delete it, click that link.\n";
                    else
                        InformationLabel.Text = "There are " + FileCounter[(int)BackUpAnyWhere.Program.FileCounterType.FileToDelete] + " old files at the destination.\n" +
                                                "In order to delete them, click that link.\n";
                }


                PopUpMessageLabel.Text = UserMessage;

                LoadingTimer.Start();
                LoadingTimer.Enabled = true;
            }
            else
            {
                X = Screen.GetWorkingArea(this).Width;
                Y = Screen.GetWorkingArea(this).Height;
                this.Location = new Point(X - this.Width, Y + this.Height);
                this.Text = "BackUp AnyWhere©";

                LoadingTimer.Start();
                LoadingTimer.Enabled = true;

                CancelAutomatedbutton.Enabled = true;
                DisplayTimer.Interval = 10000;

                CancelTimer.Interval = 1000;
                CancelTimer.Start();
                cancelMessageLabel.Font = new Font(this.Font, FontStyle.Italic | FontStyle.Underline);
                cancelMessageLabel.Text = "Automated mode will\nstart in 10 seconds.\nclick here to cancel";
            }
        }

        private void CancelAutomatedbutton_Click(object sender, EventArgs e)
        {
            DisplayTimer.Stop();
            CancelTimer.Stop();
            currentRunningCase = (int)BackUpAnyWhere.Program.UseCase.UC3_2;
            this.Close();
        }
        private void cancelMessageLabel_OnMouseClick(object sender, System.EventArgs e)
        {
            DisplayTimer.Stop();
            CancelTimer.Stop();
            currentRunningCase = (int)BackUpAnyWhere.Program.UseCase.UC3_2;
            this.Close();
        }

        private void CancelTimer_Tick(object sender, EventArgs e)
        {
            if (CancelTimerCount > 0)
            {
                CancelTimer.Interval = 1000;
                CancelTimer.Start();
                CancelTimerCount--;
                cancelMessageLabel.Text = "Automated mode will\nstart in " + CancelTimerCount.ToString() + " seconds.\nclick here to cancel";
            }
            else
                CancelTimer.Stop();
        }
    }
}
